/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.core.Application;
	
	import virtPresenter.event.*;
	import virtPresenter.event.MainWindowPreviewEvent;
	import virtPresenter.model.AS_Math;
	import virtPresenter.model.AnimationReferenceLines;
	import virtPresenter.model.FisheyeWindow;
	import virtPresenter.model.TimeStep;
	import virtPresenter.model.virtPresenterLocator;
	
	/**
	 * Command lets the presentation show up a preview of the given slide in the main presentation window.
	 * */
  	public class MainWindowMiniPreview implements ICommand {
				
		/**
		 * Handler for MainWindowMiniPreviewEvent. Lets the preview slide show up in the big slide window
		 * 
		 * @see virtPresenter.event.MainWindowMiniPreviewEvent;
		 * 
		 * */				
		private var model:virtPresenterLocator = virtPresenterLocator.getInstance();
		
		public function execute( event : CairngormEvent ): void {	
			try{
				
				var thisEvent:MainWindowMiniPreviewEvent = MainWindowMiniPreviewEvent( event );
				var requestedSlide:int = thisEvent.slideToShow;
				if (requestedSlide ==-1){
					FisheyeWindow.getInstance().setPreviewMode(false);
					model.mainWindowProxy.requestSlide(model.currentSlideID);
					model.mainWindowProxy.requestAnim(model.currentAnimStep);
					model.previewMiddleProxy.requestSlide(model.currentSlideID);
					model.previewMiddleProxy.requestAnim(model.currentAnimStep);
				}
				else
					FisheyeWindow.getInstance().setPreviewMode(true);
					model.mainWindowProxy.requestSlide(Number(requestedSlide));
					model.previewMiddleProxy.requestSlide(Number(requestedSlide));
					var anims:Array = model.xmlParser.getAnimStepsOfASlide(requestedSlide);
					var anim:TimeStep = anims.pop();
					model.mainWindowProxy.requestAnim(anim.getAnimationStep());
					model.previewMiddleProxy.requestAnim(anim.getAnimationStep());
					var newX:int = getIndex(requestedSlide+1) -1;
					model.scrubber.lv = AS_Math.calculateAnimLineValues(model.scrubber.animStepsXPos[newX],model.scrubber.animStepsXPos, model.scrubber.actualWidth);
					
			}
			catch(err:Error){}
			catch(error:ArgumentError) {
				trace ("MainWindowMiniPreview. Error in sendingLC!")
			}
		}
		
		private function getIndex(slide:int):int{
			var slides:Array = model.xmlParser.timeSteps;
			for(var i:int=0;i<slides.length;i++){
				var t:TimeStep = slides[i];
				if (t.getID() == slide)
					return i;
			}
			return slides.length;
		}
		
	}	
}